package com.zzc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzc.common.JsonResult;
import com.zzc.common.TableResult;
import com.zzc.mapper.MenuMapper;
import com.zzc.pojo.Menu;
import com.zzc.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhouzhicheng
 * @version 1.0  2020/12/29
 */
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuMapper menuMapper;
    @Override
    public JsonResult selectMenuListByUserId(Integer id) {
        //定义一个集合存储菜单信息
        List<Menu> list = new ArrayList<>();
        List<Menu> menuList = menuMapper.selectMenuListByUserId(id);
        Map<Integer,Menu> menuMap = new HashMap<>();
        for (Menu menu : menuList) {
            menuMap.put(menu.getId(),menu);
                if (menu.getParentId() == null) {
                    list.add(menu);
                }
        }
        for (Menu menu : menuList) {
            if (menu.getType()==1) {
                //有父级菜单  即为二级菜单
                if (menu.getParentId() != null && menuMap.containsKey(menu.getParentId())) {
                    //查找到二级菜单的存储在map里的父级菜单  并将二级菜单存储进去
                    menuMap.get(menu.getParentId()).getChildren().add(menu);
                }
            }
        }
        return JsonResult.getResultSuccess(list);
    }

    @Override
    public TableResult getMenuList(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        PageInfo<Menu> pageInfo = new PageInfo<>(menuMapper.getMenuList());
        return new TableResult(0,null,Long.valueOf(pageInfo.getTotal()).intValue(), pageInfo.getList());
    }

    @Override
    public JsonResult deleteMenuById(Integer[] ids) {
        try {
            if (ids == null) {
                return JsonResult.getResultFailed("参数错误！");
            }
            for (Integer id : ids) {
                menuMapper.deleteByPrimaryKey(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.getResultFailed("删除失败！");
        }
        return JsonResult.getResultSuccess();
    }

    @Override
    public JsonResult insertSelective(Menu menu) {
        if (menu == null) {
            return JsonResult.getResultFailed("参数错误！");
        }
        return menuMapper.insertSelective(menu)>0
                ? JsonResult.getResultSuccess()
                : JsonResult.getResultFailed("添加失败！");
    }

    @Override
    public JsonResult updateByPrimaryKeySelective(Menu menu) {
        if (menu == null) {
            return JsonResult.getResultFailed("参数错误！");
        }
        return menuMapper.updateByPrimaryKeySelective(menu)>0
                ? JsonResult.getResultSuccess()
                : JsonResult.getResultFailed("修改失败！");
    }

    @Override
    public Menu getMenuById(Integer id) {
        return menuMapper.getMenuById(id);
    }

    @Override
    public List<Menu> getMenus() {
        //定义一个集合存储菜单信息
        List<Menu> list = new ArrayList<>();
        List<Menu> menuList = menuMapper.getMenuList();
        Map<Integer, Menu> menuMap = new HashMap<>();
        for (Menu menu : menuList) {
            menuMap.put(menu.getId(), menu);
            if (menu.getParentId() == null) {
                list.add(menu);
            }
        }
        for (Menu menu : menuList) {
            if (menu.getType() == 1) {
                //有父级菜单  即为二级菜单
                if (menu.getParentId() != null && menuMap.containsKey(menu.getParentId())) {
                    //查找到二级菜单的存储在map里的父级菜单  并将二级菜单存储进去
                    menuMap.get(menu.getParentId()).getChildren().add(menu);
                }
            }

        }
        return list;
    }
}
